package com.along.textdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.along.textdemo.R;

import java.util.Random;

/**
 * 音频声纹的波浪动画；随机波浪动画
 */
public class SoundView extends View {

    private int soundviewCount;//竖线的个数
    private int soundviewPadding;//竖线之间的间距
    private int soundviewStartColor;
    private int soundviewEndColor;

    private int soundviewSpeed;//变化速度 次/秒
    private int soundviewWidth;//线宽度
    private int soundviewHeight;//线高度
    private Random mRandom = new Random();

    private Paint paint;
    private boolean anim = false;//是否启动动画
    private Runnable runnable;//动画的runnable


    public SoundView(Context context) {
        this(context, null);
    }

    public SoundView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SoundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**开启线程，控制动画开启
     */
    public void startAnim(){
        if (!anim){
            anim = true;
            if (runnable == null){
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        while (anim){
                            //动画
                            postInvalidate();
                            try {
                                Thread.sleep(1000 /soundviewSpeed);       //控制速度
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
            }

            post(new Runnable() {
                @Override
                public void run() {
                    new Thread(runnable).start();
                }
            });
        }
    }

    /**
     * 暂停动画
     */
    public void pauseAnim() {
        anim = false;
    }
    /**
     * 正在动画
     */
    public boolean isPlaying() {
        return anim;
    }

    public void stopAnim(){
        anim = false;
        postInvalidate();
    }


    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SoundView);
        soundviewCount = typedArray.getInt(R.styleable.SoundView_soundviewCount, 5);
        soundviewStartColor = typedArray.getColor(R.styleable.SoundView_soundviewStartColor, context.getResources().getColor(R.color.colorPrimary));
        soundviewEndColor = typedArray.getColor(R.styleable.SoundView_soundviewEndColor, context.getResources().getColor(R.color.colorPrimaryDark));
        soundviewPadding = typedArray.getDimensionPixelSize(R.styleable.SoundView_soundviewPadding, 3);
        soundviewWidth = typedArray.getDimensionPixelOffset(R.styleable.SoundView_soundviewWidth, 5);
        soundviewHeight = typedArray.getDimensionPixelOffset(R.styleable.SoundView_soundviewHeight, 15);
        soundviewSpeed = typedArray.getInt(R.styleable.SoundView_soundviewchangeSpeed, 3);
        typedArray.recycle();
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(soundviewStartColor);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //计算view大小
        int height;
        int width;
        width = soundviewWidth * soundviewCount + soundviewPadding * (soundviewCount + 1);
        height = soundviewHeight + getPaddingTop() * 2;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制音频条纹
        for (int i = 0; i < soundviewCount; ++i) {
            int distance = mRandom.nextInt(soundviewHeight / 2);
            int startX = getPaddingLeft() + (soundviewWidth + soundviewPadding) * i;
            int startY = getPaddingTop() + soundviewHeight / 2 - distance;
            int stopX = getPaddingLeft() + (soundviewWidth + soundviewPadding) * i + soundviewWidth;
            int stopY = getPaddingTop() + soundviewHeight / 2 + distance;
            canvas.drawRect(startX, startY, stopX, stopY, paint);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int mWidth = getWidth();
        int mRectHeight = getHeight();
        int mRectWidth = (int) (mWidth - soundviewPadding * (soundviewCount - 1)) / soundviewCount;
        //设置颜色渐变
        LinearGradient mLinearGradient = new LinearGradient(
                0,
                0,
                mRectWidth,
                mRectHeight,
                soundviewStartColor,
                soundviewEndColor,
                Shader.TileMode.CLAMP);
        paint.setShader(mLinearGradient);
    }

}

